package io.renren.common.config;

import io.renren.modules.noc.entity.NocLyEntity;
import io.renren.modules.noc.entity.NocTemplateEntity;
import io.renren.modules.noc.service.NocLyService;
import io.renren.modules.noc.service.NocTemplateService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TemplateInterceptor implements HandlerInterceptor {

    @Autowired
    private NocTemplateService nocTemplateService;
    @Autowired
    private NocLyService nocLyService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String reqURL = request.getRequestURL().toString();

        String tid = request.getParameter("tid");
        String ly = request.getParameter("ly");

        NocTemplateEntity nocTemplateEntity = nocTemplateService.getById(tid);
        Session session = ShiroUtils.getSession();
        session.setAttribute("tid", tid);
        session.setAttribute("ly", ly);
        session.setAttribute("template", nocTemplateEntity);

        String url = request.getRequestURL().toString();
        //验证码 闸机数据接口,错误页面，二维码生成 直接放行
        if(url.contains("/readCard")||url.contains("/createImg")||url.contains("/error")||url.contains("/createQrcode")
                ||url.contains("/invite")||url.contains("/help")||url.contains("/doInvite")||url.contains("/doHelp")
                ||url.contains("/getCheckCode")||url.contains("/getCertificate")){
            return true;
        }

        //展商数据逻辑
        if(reqURL.contains("/busi/")||reqURL.contains("/beer/")){
            return true;
        }else {
            //观众请求逻辑

            Map<String, Object> p = new HashMap<>();
            p.put("tid", tid);
            p.put("ly", ly);
            List<NocLyEntity> list = (List<NocLyEntity>) nocLyService.listByMap(p);
            //非法请求
            if (list == null || list.size() == 0) {
                return false;
            }
            //验证到期时间
            if (list.size() > 0) {
                NocLyEntity nocLyEntity = list.get(0);
                Date startTime = nocLyEntity.getStartTime();
                Date endTime = nocLyEntity.getEndTime();
                Date nowTime = new Date();
                if(startTime != null){
                    if (startTime.compareTo(nowTime) == 1) {
                        request.setAttribute("msg", "展会暂未开始,敬请期待。");
                        request.getRequestDispatcher("error").forward(request, response);
                        return false;
                    }
                }

                if (endTime != null) {
                    if (endTime.compareTo(nowTime) == -1) {
                        request.setAttribute("msg", "预登记报名结束，请直接去现场注册参观！");
                        request.getRequestDispatcher("error").forward(request, response);
                        return false;
                    }
                }
            }

        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {

    }

}